<template>
  <div id="bbbox">
    <nav class="navbar navbar-expand-lg navbar-light bg-light">
      <div class="container-fluid header">
        <h1>学员成绩</h1>
        <p>点击表格修改或录入</p>
      </div>
    </nav>

    <!-- table-striped隔行换色 -->
    <table class="table table-striped">
      <colgroup>
        <col width="14%" />
        <col width="16%" />
        <col width="14%" />
        <col width="14%" />
        <col width="14%" />
        <col width="14%" />
        <col width="14%" />
      </colgroup>
      <thead>
        <tr>
          <th scope="col">学员ID</th>
          <th scope="col">姓名</th>
          <th scope="col">第1次成绩</th>
          <th scope="col">第2次成绩</th>
          <th scope="col">第3次成绩</th>
          <th scope="col">第4次成绩</th>
          <th scope="col">第5次成绩</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="(v, key) in list" :key="key">
          <th scope="row">{{ key }}</th>
          <td>{{ v.name }}</td>
          <td
            class="score"
            v-for="(item, index) in v.score"
            :key="index"
            @dblclick="dbLclick(key, index, $event)"
          >
            <span>{{ item === null ? "未考试" : item }}</span>
            <!-- :value="item === null ? '未考试' : item" -->
            <input
              type="text"
              v-model="value"
              v-show="inputNum.id == key && inputNum.num == index"
              @blur="onBlur(key, index)"
              @keydown.enter="onKeydown(key, index)"
            />
          </td>
          <!-- <td class="score" @dblclick="dbLclick(key)"> -->
          <!-- {{ value.score[2] === null ? "未考试" : value.score[2] }} -->
          <!-- </td> -->
          <!-- <td class="score" @dblclick="dbLclick(key)"> -->
          <!-- {{ value.score[3] === null ? "未考试" : value.score[3] }} -->
          <!-- </td> -->
          <!-- <td class="score" @dblclick="dbLclick(key)"> -->
          <!-- {{ value.score[4] === null ? "未考试" : value.score[4] }} -->
          <!-- </td> -->
        </tr>
      </tbody>
    </table>
  </div>
</template>

<script>
import { allStudentListAPI, modifyStudenScoreAPI } from "@/api/article";
export default {
  data() {
    return {
      list: {},
      inputNum: { id: 0, num: 0 },
      value: 0,
    };
  },
  async created() {
    const res = await allStudentListAPI();
    // console.log("res", res);
    this.list = res.data.data;
    // console.log("list", this.list);
  },
  watch: {},
  computed: {},
  methods: {
    dbLclick(id, num, e) {
      e.target.focus();
      // 拿到下一个节点
      const aa = e.target.childNodes[0].innerText;
      this.value = aa;
      this.inputNum = { id: id, num: num };
    },
    async onBlur(key, index) {
      console.log(key * 1, index + 1, this.value);
      this.inputNum = {};
      await modifyStudenScoreAPI(key * 1, index + 1, this.value);
      // console.log(res);
      this.$router.go(0);
    },
    async onKeydown(key, index) {
      console.log(key * 1, index + 1, this.value);
      this.inputNum = {};
      await modifyStudenScoreAPI(key * 1, index + 1, this.value);
      // console.log(res);
      this.$router.go(0);
    },
  },
};
</script>

<style>
/* * {
  margin: 0;
  padding: 0;
}

body {
  background-color: #f1f1f1;
  color: #6d767e;
  padding: 15px;
  box-sizing: border-box;
  font-size: 14px;
} */
.header {
  height: 36px !important;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.header h1 {
  font-size: 18px;
  margin-bottom: 0 !important;
}

/* 表格 */
.table {
  margin: 10px auto;
  width: 100%;
  border-left: solid 1px #ccc;
  border-top: solid 1px #ccc;
  border-spacing: 0;
  border-radius: 4px;
  background-color: #fff;
  font-size: 12px;
}

.table th,
.table td {
  border-right: solid 1px #ccc;
  border-bottom: solid 1px #ccc;
  padding: 8px 0;
  text-align: center;
}

.table tr th:last-child {
  border-radius: 0 4px 0 0;
}

.table tbody tr:last-child td:first-child {
  border-radius: 0 0 0 4px;
}

.table tbody tr:last-child td:last-child {
  border-radius: 0 0 4px 0;
}
.table tr {
  line-height: 32px;
}
/* .btn-primary {
  background-color: #747ced !important;
  border-color: #747ced !important;
} */
.table .btn-sm {
  zoom: 0.8 !important;
}
.float {
  float: right;
  margin-bottom: 10px;
}
.flex {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
}
@media (min-width: 576px) {
  .modal-dialog {
    width: 385px !important;
  }
}
.flex {
  display: flex;
}
.col-auto {
  height: 30px !important;
  margin-top: 0 !important;
}
.col-form-label {
  font-size: 14px !important;
}
tbody td {
  position: relative;
}
tbody input {
  /* display: none; */
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  border: none;
  background-color: #fff;
  outline-color: blue;
  text-align: center;
}
</style>